using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using GuavaData;

namespace GuavaLib
{
    public partial class InitialColumnDefinitionForm : Form
    {
        public InitialColumnDefinitionForm()
        {
            InitializeComponent();
            foreach (GuavaData.Domain.GNumericDomains gnbd in Enum.GetValues(typeof(GuavaData.Domain.GNumericDomains))) this.cmbNumericType.Items.Add(gnbd);
            this.cmbNumericType.SelectedItem = 0;
        }

        public GuavaData.Domain.Domain ColumnDomain
        {
            get
            {
                if (this.rBoolean.Checked)
                {
                    return new GuavaData.Domain.Boolean();
                }
                if (this.rString.Checked)
                {
                    int i;
                    if (this.txtMaximumLength.Text.Length > 0 && Int32.TryParse(this.txtMaximumLength.Text, out i)) return new GuavaData.Domain.String(i);
                    return new GuavaData.Domain.String();
                }
                if (this.rLookup.Checked)
                {
                    return new GuavaData.Domain.Lookup(this.txtTableName.Text, this.txtView.Text);
                }
                if (this.rDateTime.Checked)
                {
                    return new GuavaData.Domain.DateTime();
                }
                if (this.rEnumerated.Checked)
                {
                    return new GuavaData.Domain.Enumerated(this.txtEnumItems.Lines);
                }
                if (this.rNumeric.Checked)
                {
                    switch ((GuavaData.Domain.GNumericDomains)this.cmbNumericType.SelectedItem)
                    {
                        case GuavaData.Domain.GNumericDomains.FixedPoint:
                            return new GuavaData.Domain.Decimal();
                        case GuavaData.Domain.GNumericDomains.FloatingPointDouble:
                            return new GuavaData.Domain.Double();
                        case GuavaData.Domain.GNumericDomains.FloatingPointSingle:
                            return new GuavaData.Domain.Single();
                        case GuavaData.Domain.GNumericDomains.Integer1Byte:
                            return new GuavaData.Domain.TinyInteger();
                        case GuavaData.Domain.GNumericDomains.Integer2Byte:
                            return new GuavaData.Domain.SmallInteger();
                        case GuavaData.Domain.GNumericDomains.Integer4Byte:
                            return new GuavaData.Domain.Integer();
                        case GuavaData.Domain.GNumericDomains.Integer8Byte:
                            return new GuavaData.Domain.BigInteger();
                    }
                }
                return null;
            }
        }

        private void rBoolean_CheckedChanged(object sender, EventArgs e)
        {
            if (sender == this.rBoolean)
            {
                this.txtView.Enabled = false;
                this.txtTableName.Enabled = false;
                this.txtMaximumLength.Enabled = false;
                this.txtEnumItems.Enabled = false;
                this.txtMaxValue.Enabled = false;
                this.txtMinValue.Enabled = false;
                this.cmbNumericType.Enabled = false;
            }
            return;
        }

        private void rString_CheckedChanged(object sender, EventArgs e)
        {
            if (sender == this.rString)
            {
                this.txtView.Enabled = false;
                this.txtTableName.Enabled = false;
                this.txtMaximumLength.Enabled = true;
                this.txtEnumItems.Enabled = false;
                this.txtMaxValue.Enabled = false;
                this.txtMinValue.Enabled = false;
                this.cmbNumericType.Enabled = false;
            }
            return;
        }

        private void rLookup_CheckedChanged(object sender, EventArgs e)
        {
            if (sender == this.rLookup)
            {
                this.txtView.Enabled = true;
                this.txtTableName.Enabled = true;
                this.txtMaximumLength.Enabled = false;
                this.txtEnumItems.Enabled = false;
                this.txtMaxValue.Enabled = false;
                this.txtMinValue.Enabled = false;
                this.cmbNumericType.Enabled = false;
            }
            return;
        }

        private void rEnumerated_CheckedChanged(object sender, EventArgs e)
        {
            this.txtView.Enabled = false;
            this.txtTableName.Enabled = false;
            this.txtMaximumLength.Enabled = false;
            this.txtEnumItems.Enabled = true;
            this.txtMaxValue.Enabled = false;
            this.txtMinValue.Enabled = false;
            this.cmbNumericType.Enabled = false;
        }

        private void rDateTime_CheckedChanged(object sender, EventArgs e)
        {
            this.txtView.Enabled = false;
            this.txtTableName.Enabled = false;
            this.txtMaximumLength.Enabled = false;
            this.txtEnumItems.Enabled = false;
            this.txtMaxValue.Enabled = false;
            this.txtMinValue.Enabled = false;
            this.cmbNumericType.Enabled = false;
        }

        private void rNumeric_CheckedChanged(object sender, EventArgs e)
        {
            this.txtView.Enabled = false;
            this.txtTableName.Enabled = false;
            this.txtMaximumLength.Enabled = false;
            this.txtEnumItems.Enabled = false;
            this.txtMaxValue.Enabled = true;
            this.txtMinValue.Enabled = true;
            this.cmbNumericType.Enabled = true;
        }

        public string ColumnName
        {
            get
            {
                return this.txtColumnName.Text;
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (this.txtColumnName.Text.Length > 0)
            {
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            else
                MessageBox.Show("Please enter a column name.");
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }
    }
}